// 图形库
#include <graphics.h>
// 标准库
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <conio.h>

#define PI 3.14159265358979323846
using namespace std;


void drawScaleA(float cx, float cy, float radius)
{
	// 核心修改：添加比例因子（基于原始radius=400）
	float scale = radius / 200;
	
	int centerX = cx;
	int centerY = cy;
	
	float innerRadius = 125.0f * scale;  // 内环半径（原0.8比例）
	
	
	// 刻度参数化 ================================================
	
	// 刻度1（每10度一个）
	setcolor(WHITE);
	for (int i = 0; i < 3; ++i) // 保持原3个刻度
	{
		float baseAngle = 70 * PI / 180;
		float currentAngle = baseAngle + i * PI / 36.0f;
		

		// 刻度线参数化
		float startRadius = innerRadius;
		float endRadius = innerRadius + scale * 7; // 刻度长度占5%半径
			
		int x1 = centerX + startRadius * cos(currentAngle);
		int y1 = centerY - startRadius * sin(currentAngle);
		int x2 = centerX + endRadius * cos(currentAngle);
		int y2 = centerY - endRadius * sin(currentAngle);
			
		// 线宽参数化
		setlinestyle(SOLID_LINE, 0, scale * 1);
		ege_line(x1, y1, x2, y2);
	}
	
	
	for (int i = 0; i < 3; ++i) // 保持原3个刻度
	{
		float baseAngle = 100 * PI / 180;
		float currentAngle = baseAngle + i * PI / 36.0f;
		
		
		// 刻度线参数化
		float startRadius = innerRadius;
		float endRadius = innerRadius + scale * 7; // 刻度长度占5%半径
		
		int x1 = centerX + startRadius * cos(currentAngle);
		int y1 = centerY - startRadius * sin(currentAngle);
		int x2 = centerX + endRadius * cos(currentAngle);
		int y2 = centerY - endRadius * sin(currentAngle);
		
		// 线宽参数化
		setlinestyle(SOLID_LINE, 0, scale * 1);
		ege_line(x1, y1, x2, y2);
	}
	
	
	for (int i = 0; i < 2; ++i) // 保持原3个刻度
	{
		float baseAngle = 45 * PI / 180;
		float currentAngle = baseAngle + i * PI / 2.0f;
		
		
		// 刻度线参数化
		float startRadius = innerRadius;
		float endRadius = innerRadius + scale * 7; // 刻度长度占5%半径
		
		int x1 = centerX + startRadius * cos(currentAngle);
		int y1 = centerY - startRadius * sin(currentAngle);
		int x2 = centerX + endRadius * cos(currentAngle);
		int y2 = centerY - endRadius * sin(currentAngle);
		
		// 线宽参数化
		setlinestyle(SOLID_LINE, 0, scale * 1);
		ege_line(x1, y1, x2, y2);
	}
	
	
	//刻度2（每30度一个）
	for (int i = 0; i < 5; ++i) // 保持原2组刻度
	{
		float baseAngle = 30 * PI / 180;
		float currentAngle = baseAngle + i * PI / 6.0f; // 90度间隔 (PI/18)
		
		// 刻度线参数化
		float startRadius = innerRadius;
		float endRadius = innerRadius + scale * 13;
		
		int x1 = centerX + startRadius * cos(currentAngle);
		int y1 = centerY - startRadius * sin(currentAngle);
		int x2 = centerX + endRadius * cos(currentAngle);
		int y2 = centerY - endRadius * sin(currentAngle);
		
		// 线宽参数化）
		setlinestyle(SOLID_LINE, 0, scale*2);
		ege_line(x1, y1, x2, y2);
	}
	
	
	int width = radius / 30;
	
	setcolor(WHITE);
	setfillcolor(WHITE);
	setlinestyle(SOLID_LINE,0,2*scale);
	ege_line(centerX, centerY - radius + 65 *scale, centerX - width / 2, centerY - radius + 58 * scale);
	ege_line(centerX, centerY - radius + 65 *scale, centerX + width / 2, centerY - radius + 58 *scale);
	ege_line(centerX - width / 2, centerY - radius + 58 *scale, centerX + width / 2, centerY - radius + 58 *scale);
	
	int Points[] = {centerX - width / 2, centerY - radius + 58 *scale,
		centerX + width / 2, centerY - radius + 58 *scale, centerX, centerY - radius + 65 *scale,
		centerX - width / 2, centerY - radius + 58 *scale};
	fillpoly(4, Points);
	
	setcolor(BLACK);
	setfillcolor(BLACK);
	setlinestyle(SOLID_LINE, 0, 2*scale);
	ege_line(cx-30*scale,cy,cx-15*scale,cy+15*scale);
	ege_line(cx-15*scale,cy+15*scale,cx,cy);
	ege_line(cx+30*scale,cy,cx+15*scale,cy+15*scale);
	ege_line(cx+15*scale,cy+15*scale,cx,cy);
	
	setlinestyle(SOLID_LINE, 0, 1*scale);
	ege_line(cx+65*scale,cy,cx+80*scale,cy+15*scale);
	ege_line(cx+80*scale,cy+15*scale,cx+110*scale,cy+15*scale);
	
	setcolor(EGERGB(242,121,37));
	setfillcolor(EGERGB(242,121,37));
	
	ege_fillcircle(cx,cy,3*scale);
	setlinestyle(SOLID_LINE, 0, 3*scale);
	ege_line(cx-30*scale,cy,cx-65*scale,cy);
	ege_line(cx+30*scale,cy,cx+65*scale,cy);
	
	
	//绘制右下角白色按钮
	setcolor(WHITE);
	setfillcolor(WHITE);
	fillcircle(cx+radius*0.65,cy+radius*0.65,27*scale);
	
	setcolor(BLACK);
	setfillcolor(BLACK);
	ege_fillcircle(cx+radius*0.65,cy+radius*0.65,3*scale);
}

void drawBallBackground(float x, float y, float radius)
{
	
	float scale = radius / 200.0f;
	
	ege_path* path = ege_path_create();

	ege_path_addarc(path, x - 150 * scale, y - 170 * scale, 300 * scale, 150 * scale, 180, 180);
	ege_path_addline(path, x + 150 * scale, y - 70 * scale, x + 150 * scale, y + 70 * scale);
	ege_path_addarc(path, x - 150 * scale, y - 100 * scale, 300 * scale, 260 * scale, 0, 180);
	ege_path_addline(path, x - 150 * scale, y - 70 * scale, x - 150 * scale, y + 70 * scale);
	setfillcolor(EGERGB(0, 0, 0));
	ege_fillpath(path);
		
}

void drawoutborad(double x, double y, double r)
{
// 表盘最外层（深色边框）
	setfillcolor(EGERGB(19, 21, 21));
	ege_point x_y1[] = {
		// 左上水平边中点
		{x - (int)(1.22 * r), y - (int)(0.96 * r)},
		// 顶部尖角左侧点
		{x - (int)(0.90 * r), y - (int)(1.23 * r)},
		// 顶部尖角右侧点
		{x + (int)(0.90 * r), y - (int)(1.23 * r)},
		// 右上水平边中点
		{x + (int)(1.22 * r), y - (int)(0.96 * r)},
		// 右下水平边中点
		{x + (int)(1.22 * r), y + (int)(0.96 * r)},
		// 底部尖角右侧点
		{x + (int)(0.90 * r), y + (int)(1.22 * r)},
		// 底部尖角左侧点
		{x - (int)(0.90 * r), y + (int)(1.22 * r)},
		// 左下水平边中点
		{x - (int)(1.22 * r), y + (int)(0.96 * r)}
	};
	ege_fillpoly(8, x_y1);
	
// 表盘第二层（浅绿色边框）
	setfillcolor(EGERGB(140, 160, 140));
	ege_point x_y2[] = {
		// 左上水平边中点（内缩0.01r）
		{x - (int)(1.21 * r), y - (int)(0.95 * r)},
		// 顶部尖角左侧点（内缩0.01r）
		{x - (int)(0.89 * r), y - (int)(1.22 * r)},
		// 顶部尖角右侧点（内缩0.01r）
		{x + (int)(0.89 * r), y - (int)(1.22 * r)},
		// 右上水平边中点（内缩0.01r）
		{x + (int)(1.21 * r), y - (int)(0.95 * r)},
		// 右下水平边中点（内缩0.01r）
		{x + (int)(1.21 * r), y + (int)(0.95 * r)},
		// 底部尖角右侧点（内缩0.01r）
		{x + (int)(0.89 * r), y + (int)(1.21 * r)},
		// 底部尖角左侧点（内缩0.01r）
		{x - (int)(0.89 * r), y + (int)(1.21 * r)},
		// 左下水平边中点（内缩0.01r）
		{x - (int)(1.21 * r), y + (int)(0.95 * r)}
	};
	ege_fillpoly(8, x_y2);
	
// 表盘第三层（灰色边框）
	setfillcolor(EGERGB(160, 160, 160));
	ege_point x_y3[] = {
		// 左上水平边中点（内缩0.01r）
		{x - (int)(1.21 * r), y - (int)(0.95 * r)},
		// 顶部尖角左侧点（内缩0.01r）
		{x - (int)(0.89 * r), y - (int)(1.22 * r)},
		// 顶部尖角右侧点（内缩0.01r）
		{x + (int)(0.89 * r), y - (int)(1.22 * r)},
		// 右上水平边中点（内缩0.01r）
		{x + (int)(1.21 * r), y - (int)(0.95 * r)},
		// 右下水平边中点（内缩0.01r）
		{x + (int)(1.21 * r), y + (int)(0.95 * r)},
		// 底部尖角右侧点（高度略调整）
		{x + (int)(0.88 * r), y + (int)(1.20 * r)},
		// 底部尖角左侧点（高度略调整）
		{x - (int)(0.88 * r), y + (int)(1.20 * r)},
		// 左下水平边中点（宽度略调整）
		{x - (int)(1.20 * r), y + (int)(0.94 * r)}
	};
	ege_fillpoly(8, x_y3);
	
// 表盘最内层（深灰色边框）
	setfillcolor(EGERGB(130, 130, 130));
	ege_point x_y[] = {
		// 左上水平边中点（进一步内缩0.01r）
		{x - (int)(1.20 * r), y - (int)(0.94 * r)},
		// 顶部尖角左侧点（进一步内缩0.01r）
		{x - (int)(0.88 * r), y - (int)(1.21 * r)},
		// 顶部尖角右侧点（进一步内缩0.01r）
		{x + (int)(0.88 * r), y - (int)(1.21 * r)},
		// 右上水平边中点（进一步内缩0.01r）
		{x + (int)(1.20 * r), y - (int)(0.94 * r)},
		// 右下水平边中点（最内层，内缩0.01r）
		{x + (int)(1.19 * r), y + (int)(0.93 * r)},
		// 底部尖角右侧点（高度略调整）
		{x + (int)(0.88 * r), y + (int)(1.18 * r)},
		// 底部尖角左侧点（高度略调整）
		{x - (int)(0.87 * r), y + (int)(1.18 * r)},
		// 左下水平边中点（宽度略调整）
		{x - (int)(1.19 * r), y + (int)(0.92 * r)}
	};
	ege_fillpoly(8, x_y);
	
//	y = y + r / 10 + r / 20;	
//	y = y - r / 10 - r / 20;
	
}


struct RotatingSphere {
	float x, y;     // 屏幕坐标中心
	float radius;   // 球体半径
	float angleX;   // 绕X轴旋转角度（俯仰）
	float angleY;   // 绕Y轴旋转角度（横滚）
};

	
ege_point calculateSpherePoint(float cx, float cy, float r, 
	float phi, float theta,
	float angleX, float angleY) {
		// 球面坐标转3D坐标（注意Y轴向上）
		float x = r * sin(phi) * cos(theta);
		float y = r * cos(phi);  // Y轴向上
		float z = r * sin(phi) * sin(theta);
		
		// 绕X轴旋转（修正后的旋转方向）
		float y_rotX = y * cos(angleX) - z * sin(angleX);
		float z_rotX = y * sin(angleX) + z * cos(angleX);
		
		// 绕Y轴旋转
		float x_rotY = x * cos(angleY) + z_rotX * sin(angleY);
		float z_rotY = -x * sin(angleY) + z_rotX * cos(angleY);
		
		// 添加透视效果（考虑z坐标）
		float persp = 1.0f + z_rotY/(r*2);  // 透视系数
		return {cx + x_rotY/persp, cy - y_rotX/persp}; // 注意：屏幕Y轴向下
	}

void drawRotatingSphere(const RotatingSphere& sphere) 
{
	float scale = sphere.radius / 110.0f;
	const int SEGMENTS = 36;
	const int PARALLELS = 18;
	
	vector<vector<ege_point>> points(PARALLELS+1, vector<ege_point>(SEGMENTS+1));
	vector<vector<color_t>> colors(PARALLELS+1, vector<color_t>(SEGMENTS+1));
	
	// 计算顶点和颜色
	for (int p = 0; p <= PARALLELS; p++) {
		float phi = PI * p / PARALLELS;
		for (int s = 0; s <= SEGMENTS; s++) {
			float theta = 2 * PI * s / SEGMENTS;
			points[p][s] = calculateSpherePoint(
				sphere.x, sphere.y, sphere.radius,
				phi, theta, sphere.angleX, sphere.angleY);
			
			// 改进的光照计算
			float light = 0.5f * (cos(phi) * cos(sphere.angleX) + 
				sin(theta) * sin(sphere.angleY) + 1);
			if (p < PARALLELS/2) {
				colors[p][s] = RGB(105, 170, 200);
			} else {
				int gray = 25 + (int)(10 * light);
				colors[p][s] = EGERGB(gray, gray, gray);
			}
		}
	}
	
	// 绘制球面（背面剔除简化版）
	for (int p = 0; p < PARALLELS; p++) 
	{
		for (int s = 0; s < SEGMENTS; s++) 
		{
			// 简单的背面剔除（检查法线方向）
			float nx = sin(PI * p / PARALLELS) * cos(2 * PI * s / SEGMENTS);
			float nz = sin(PI * p / PARALLELS) * sin(2 * PI * s / SEGMENTS);
			//if (nz < 0) continue;  // 跳过背对摄像机的面
			
			ege_point quad[4] = {
				points[p][s], points[p][s+1], 
				points[p+1][s+1], points[p+1][s]
			};
			
			// 计算平均颜色
			int r=0, g=0, b=0;
			for (int i = 0; i < 4; i++) {
				color_t c = colors[p + i/2][s + i%2];
				r += GetRValue(c);
				g += GetGValue(c);
				b += GetBValue(c);
			}
			setfillcolor(EGERGB(r/4, g/4, b/4));
			ege_fillpoly(4, quad);
		}
	}
	
	// 绘制赤道环线
	int POINTS = 200;
	vector<ege_point> equator(POINTS + 2);
	for (int i = 0; i <= POINTS; i++) 
	{
		float theta = 2 * PI * i / POINTS;
		equator[i] = calculateSpherePoint(
			sphere.x, sphere.y, sphere.radius,
			PI/2, theta, sphere.angleX, sphere.angleY);
	}
	setcolor(WHITE);
	setlinestyle(SOLID_LINE, 0, 3);
	for (int i = 0; i < POINTS; i++) 
	{
		ege_line(equator[i].x, equator[i].y, equator[i+1].x, equator[i+1].y);
	}
	
	
	//上面
	vector<ege_point> point(POINTS + 2);
	for (int i = 0; i <= POINTS; i++) 
	{
		float theta =  PI / 2 * i / POINTS+PI/2.5;
		point[i] = calculateSpherePoint(
			sphere.x, sphere.y, sphere.radius,
			PI / 3, theta, sphere.angleX, sphere.angleY);
		
	}
	setcolor(BLACK);
	setlinestyle(SOLID_LINE, 0, 2);
	
	//10
	for (int i = 0; i < POINTS; i++) 
	{
		ege_line(point[i].x, point[i].y, point[i+1].x, point[i+1].y);
	}
		
	//20	
	for (int i = 0; i < POINTS; i++) 
	{
		ege_line(point[i].x+10*scale, point[i].y-40*scale, point[i+1].x+10*scale, point[i+1].y-40*scale);
	}
	
	//30	
	for (int i = 0; i < POINTS; i++) 
	{
		ege_line(point[i].x+15*scale, point[i].y-70*scale, point[i+1].x+15*scale, point[i+1].y-70*scale);
	}

	//上方短刻度	
	vector<ege_point> point1(POINTS + 2);
	for (int i = 0; i <= POINTS; i++) 
	{
		float theta =  PI / 4* i / POINTS+PI/2;
		point1[i] = calculateSpherePoint(
			sphere.x, sphere.y, sphere.radius,
			PI / 180 * 75, theta, sphere.angleX, sphere.angleY);
		
	}
	for (int i = 0; i < POINTS; i++) 
	{
		ege_line(point1[i].x, point1[i].y, point1[i+1].x, point1[i+1].y);
	}
	for (int i = 0; i < POINTS; i++) 
	{
		ege_line(point1[i].x, point1[i].y-40*scale, point1[i+1].x, point1[i+1].y-40*scale);
	}
	for (int i = 0; i < POINTS; i++) 
	{
		ege_line(point1[i].x, point1[i].y-75*scale, point1[i+1].x, point1[i+1].y-75*scale);
	}
	
	
	
	//下面
	vector<ege_point> point2(POINTS + 2);
	for (int i = 0; i <= POINTS; i++) 
	{
		float theta =  PI / 2 * i / POINTS+PI/2.5;
		point2[i] = calculateSpherePoint(
			sphere.x, sphere.y, sphere.radius,
			PI *2 / 3, theta, sphere.angleX, sphere.angleY);
		
	}
	setcolor(WHITE);
	setlinestyle(SOLID_LINE, 0, 2);
	
	//10
	for (int i = 0; i < POINTS; i++) 
	{
		ege_line(point2[i].x, point2[i].y, point2[i+1].x, point2[i+1].y);
	}
	
	//20	
	for (int i = 0; i < POINTS; i++) 
	{
		ege_line(point2[i].x+10*scale, point2[i].y+40*scale, point2[i+1].x+10*scale, point2[i+1].y+40*scale);
	}
	
	//30	
	for (int i = 0; i < POINTS; i++) 
	{
		ege_line(point2[i].x+15*scale, point2[i].y+70*scale, point2[i+1].x+15*scale, point2[i+1].y+70*scale);
	}
	
	//下方短刻度	
	vector<ege_point> point3(POINTS + 2);
	for (int i = 0; i <= POINTS; i++) 
	{
		float theta =  PI / 4* i / POINTS+PI/2;
		point3[i] = calculateSpherePoint(
			sphere.x, sphere.y, sphere.radius,
			PI / 180 * 105, theta, sphere.angleX, sphere.angleY);
		
	}
	for (int i = 0; i < POINTS; i++) 
	{
		ege_line(point3[i].x, point3[i].y, point3[i+1].x, point3[i+1].y);
	}
	for (int i = 0; i < POINTS; i++) 
	{
		ege_line(point3[i].x, point3[i].y+40*scale, point3[i+1].x, point3[i+1].y+40*scale);
	}
	for (int i = 0; i < POINTS; i++) 
	{
		ege_line(point3[i].x, point3[i].y+75*scale, point3[i+1].x, point3[i+1].y+75*scale);
	}
}


void drawScaleNumbersA(float cx, float cy, float radius, float updown, float angle) 
{
	updown = updown * 180 / PI;
	setcolor(WHITE);
	setfont(radius/7, 0, "Arial"); // 根据球体大小调整字体
	
	const char* numbers1[] = {"30", "20", "10"};
	const float angles1[] = {10-updown, 35-updown, 55-updown}; // 对应刻度角度(度)
	
	for(int i = 0; i < 3; i++) 
	{
		float phi = angles1[i] * PI / 180;          // 赤道位置
		float theta = PI; // 转换为弧度
		
		// 3D坐标计算（考虑旋转）
		float x3d = radius * sin(phi) * cos(theta);
		float y3d = radius * cos(phi);
		float z3d = radius * sin(phi) * sin(theta);
		
		// 绕Y轴旋转
		float xRot = x3d * cos(angle) + z3d * sin(angle);
		float zRot = -x3d * sin(angle) + z3d * cos(angle);
		
		// 简单透视投影
		float persp = 1.0 + zRot/(radius*2);
		int x = cx + xRot/persp;
		int y = cy - y3d/persp;
		
		// 背面剔除（只显示正面的数字）
		if(zRot > radius*0.1)
		{
			ege_outtextxy(x, y, numbers1[i]);
		}
	}

	const char* numbers2[] = {"30", "20", "10"};
	const float angles2[] = {15+updown, 45+updown, 65+updown}; // 对应刻度角度(度)
	
	for(int i = 0; i < 3; i++) 
	{
		float phi = angles2[i] * PI / 180;          // 赤道位置
		float theta = PI; // 转换为弧度
		
		// 3D坐标计算（考虑旋转）
		float x3d = radius * sin(phi) * cos(theta);
		float y3d = radius * cos(phi);
		float z3d = radius * sin(phi) * sin(theta);
		
		// 绕Y轴旋转
		float xRot = x3d * cos(angle) + z3d * sin(angle);
		float zRot = -x3d * sin(angle) + z3d * cos(angle);
		
		// 简单透视投影
		float persp = 1.0 + zRot/(radius*2);
		int x = cx + xRot/persp;
		int y = cy + y3d/persp;
		
		// 背面剔除（只显示正面的数字）
		if(zRot > radius*0.1)
		{
			ege_outtextxy(x, y, numbers2[i]);
		}
	}
}

void drawAttitudeBoard(float AttitudeCenterX, float AttitudeCenterY, float AttitudeRadius, RotatingSphere sphere)
{
	drawoutborad(AttitudeCenterX,AttitudeCenterY,AttitudeRadius*0.85);
	drawBallBackground(AttitudeCenterX,AttitudeCenterY,AttitudeRadius);
	// 绘制球体
	drawRotatingSphere(sphere);
	drawScaleNumbersA(AttitudeCenterX, AttitudeCenterY, AttitudeRadius*0.625, sphere.angleX, sphere.angleY);
	drawScaleA(AttitudeCenterX,AttitudeCenterY,AttitudeRadius);	//绘制上方刻度
}

int main()
{
	// 初始化图形窗口
	int width = 800, length = 600;
	initgraph(width, length);
	ege_enable_aa(true);
	//仪表背景色
	setrendermode(RENDER_MANUAL);
	setbkcolor(EGERGB(180, 180, 180));
	cleardevice();
	float AttitudeCenterX = 400;
	float AttitudeCenterY = 300;
	float AttitudeRadius = 200;
	
	RotatingSphere sphere = {AttitudeCenterX, AttitudeCenterY, AttitudeRadius*0.55, 0, 20*PI/180};
	
	//sphere.angleX为俯仰角，sphere.angleY为横滚角,后续需要数据传输实现动态效果
	while (1) 
	{
		// 清屏
		cleardevice();
		drawAttitudeBoard(AttitudeCenterX, AttitudeCenterY, AttitudeRadius,sphere);
;		delay_ms(10);
	}
	
	getch();
	closegraph();
	return 0;
}


//
//#include <graphics.h>
//#include <math.h>
//#include <time.h>
//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//#include <algorithm>
//#include <vector>
//#include <conio.h>
//
//#define PI 3.14159265358979323846
//using namespace std;
//
//struct RotatingSphere {
//	float x, y;     // 屏幕坐标中心
//	float radius;   // 球体半径
//	float angleX;   // 绕X轴旋转角度（俯仰）
//	float angleY;   // 绕Y轴旋转角度（横滚）
//};
//

//ege_point calculateSpherePoint(float cx, float cy, float r, 
//	float phi, float theta,
//	float angleX, float angleY) {
//		// 球面坐标转3D坐标（注意Y轴向上）
//		float x = r * sin(phi) * cos(theta);
//		float y = r * cos(phi);  // Y轴向上
//		float z = r * sin(phi) * sin(theta);
//		
//		// 绕X轴旋转（修正后的旋转方向）
//		float y_rotX = y * cos(angleX) - z * sin(angleX);
//		float z_rotX = y * sin(angleX) + z * cos(angleX);
//		
//		// 绕Y轴旋转
//		float x_rotY = x * cos(angleY) + z_rotX * sin(angleY);
//		float z_rotY = -x * sin(angleY) + z_rotX * cos(angleY);
//		
//		// 添加透视效果（考虑z坐标）
//		float persp = 1.0f + z_rotY/(r*2);  // 透视系数
//		return {cx + x_rotY/persp, cy - y_rotX/persp}; // 注意：屏幕Y轴向下
//	}
//
//void drawRotatingSphere(const RotatingSphere& sphere) {
//	const int SEGMENTS = 36;
//	const int PARALLELS = 18;
//	
//	vector<vector<ege_point>> points(PARALLELS+1, vector<ege_point>(SEGMENTS+1));
//	vector<vector<color_t>> colors(PARALLELS+1, vector<color_t>(SEGMENTS+1));
//	
//	// 计算顶点和颜色
//	for (int p = 0; p <= PARALLELS; p++) {
//		float phi = PI * p / PARALLELS;
//		for (int s = 0; s <= SEGMENTS; s++) {
//			float theta = 2 * PI * s / SEGMENTS;
//			points[p][s] = calculateSpherePoint(
//				sphere.x, sphere.y, sphere.radius,
//				phi, theta, sphere.angleX, sphere.angleY);
//			
//			// 改进的光照计算
//			float light = 0.5f * (cos(phi) * cos(sphere.angleX) + 
//				sin(theta) * sin(sphere.angleY) + 1);
//			if (p > PARALLELS/2) {
//				colors[p][s] = RGB(105, 170, 200);
//			} else {
//				int gray = 30 + (int)(40 * light);
//				colors[p][s] = EGERGB(gray, gray, gray);
//			}
//		}
//	}
//	
//	// 绘制球面（背面剔除简化版）
//	for (int p = 0; p < PARALLELS; p++) 
//	{
//		for (int s = 0; s < SEGMENTS; s++) 
//		{
//			// 简单的背面剔除（检查法线方向）
//			float nx = sin(PI * p / PARALLELS) * cos(2 * PI * s / SEGMENTS);
//			float nz = sin(PI * p / PARALLELS) * sin(2 * PI * s / SEGMENTS);
//			//if (nz > 0) continue;  // 跳过背对摄像机的面
//			
//			ege_point quad[4] = {
//				points[p][s], points[p][s+1], 
//				points[p+1][s+1], points[p+1][s]
//			};
//			
//			// 计算平均颜色
//			int r=0, g=0, b=0;
//			for (int i = 0; i < 4; i++) {
//				color_t c = colors[p + i/2][s + i%2];
//				r += GetRValue(c);
//				g += GetGValue(c);
//				b += GetBValue(c);
//			}
//			setfillcolor(EGERGB(r/4, g/4, b/4));
//			ege_fillpoly(4, quad);
//		}
//	}
//	
//	// 绘制赤道环线
//	const int POINTS = 100;
//	vector<ege_point> equator(POINTS + 1);
//	for (int i = 0; i <= POINTS; i++) {
//		float theta = 2 * PI * i / POINTS;
//		equator[i] = calculateSpherePoint(
//			sphere.x, sphere.y, sphere.radius,
//			PI/2, theta, sphere.angleX, sphere.angleY);
//	}
//	
//	setcolor(WHITE);
//	setlinestyle(SOLID_LINE, 0, 3);
//	for (int i = 0; i < POINTS; i++) {
//		ege_line(equator[i].x, equator[i].y, equator[i+1].x, equator[i+1].y);
//	}
//}
//
//int main() 
//{
//	initgraph(800, 600);
//	ege_enable_aa(true);
//	cleardevice();
//	
//	RotatingSphere sphere = {400, 300, 150, 0, 0};
//	const float ROT_SPEED = 0.02f;
//	
//	while (1) 
//	{
//		cleardevice();
//		
//		// 键盘控制（修正了X轴旋转方向）
//		if (GetAsyncKeyState(VK_UP))    sphere.angleX -= ROT_SPEED; // 上键：抬头
//		if (GetAsyncKeyState(VK_DOWN))  sphere.angleX += ROT_SPEED; // 下键：低头
//		if (GetAsyncKeyState(VK_LEFT))  sphere.angleY -= ROT_SPEED; // 左键：左滚
//		if (GetAsyncKeyState(VK_RIGHT)) sphere.angleY += ROT_SPEED; // 右键：右滚
//		
//		drawRotatingSphere(sphere);
//		
//		// 显示操作说明
//		setcolor(WHITE);
//		outtextxy(20, 20, "X轴旋转(俯仰): UP/DOWN");
//		outtextxy(20, 40, "Y轴旋转(横滚): LEFT/RIGHT");
//		
//		char status[100];
//		sprintf(status, "当前角度: X=%.1f° Y=%.1f°", 
//			sphere.angleX*180/PI, sphere.angleY*180/PI);
//		outtextxy(20, 60, status);
//		
//		delay_ms(10);
//	}
//	
//	closegraph();
//	return 0;
//}